// pages/serviceAlert/serviceAlert.js

const API = require('../../utils/ajax.js')
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    inputShowed: false,  //初始文本框不显示内容
    searchList: [
      { name: '湘A12345', id: 1 },
      { name: '湘A12345', id: 2 },
      { name: '湘A12345', id: 3 },
      { name: '湘A12345', id: 4 },
      { name: '湘A12345', id: 5 },
    ],
    uid: 7799,
    carList: [],
    inputVal: "",

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.carList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  async carList() {
    wx.showLoading({
      "mask": true,
      "title": "加载中..."
    });
    const res = await API.getCarList();
    let carList = [];
    wx.hideLoading()
    if (res.code === 0) {
      const _carList = res.data;

      carList = carList.concat(_carList)
    }
    this.setData({
      carList: carList
    })
  },
  toCarDetail: function (e) {
    let vid=e.currentTarget.dataset.id
    wx.navigateTo({
      url:`/pages/trackReplayDetail/trackReplayDetail?vid=${vid}`
    });
    var inputVal = this.data.inputVal
    inputVal = ""
    this.carList()
    this.setData({
      inputVal
    })

  },
  bindinput: function (e) {
    var inputVal = e.detail.value
    let carList = [];
    wx.hideLoading()
    this.setData({
      inputVal: inputVal
    })
  },
  async bindconfirm(e) {
    var inputVal = e.detail.value;
    wx.showLoading({
      "mask": true,
      "title": "加载中..."
    });
    const res = await API.getCarList();
    wx.hideLoading()
    var carList = [];
    if (res.code === 0) {
      const _carList = res.data.filter(ele => {
        if (inputVal === '') {
          wx.showToast({
            title: '不能为空',
          })
          return true;
        }
        return ele.name.includes(inputVal);
      })
      carList = carList.concat(_carList)
    }
    this.setData({
      carList: carList,
      inputVal: inputVal
    })
  }
})